#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author   : zhongxin
# @Time     : 2022/9/2 11:56
# @File     : serializers.py
# @Project  : PyCharm
import datetime

import pytz
from django.db.models import Sum
from rest_framework import serializers

from product.serializers import ProductAllModelSerializer
from utils import validates
from warehouse.models import Warehouse


class WarehouseModelSerializer(serializers.ModelSerializer):
    product_id = serializers.IntegerField(
        write_only=True,
        help_text='产品信息ID',
        validators=[validates.whether_existed_product_id]
    )
    product = ProductAllModelSerializer(read_only=True, many=False)
    remaining = serializers.SerializerMethodField(label="剩余天数", read_only=True, help_text="剩余天数")
    remaining_day = serializers.SerializerMethodField(label="保质期日期", read_only=True, help_text="保质期日期")
    total = serializers.SerializerMethodField(label="总库存", read_only=True, help_text="一个产品的总库存")
    warehouse1 = serializers.SerializerMethodField(label="出库", read_only=True, help_text="一个产品的总出库")
    warehouse2 = serializers.SerializerMethodField(label="入库", read_only=True, help_text="一个产品的总入库")
    warehouse1_time = serializers.SerializerMethodField(label="已经出库时间", read_only=True, help_text="出库距离当前的时间")
    warehouse2_time = serializers.SerializerMethodField(label="已经入库时间", read_only=True, help_text="入库距离当前的时间")

    class Meta:
        model = Warehouse
        exclude = ('is_delete',)
        extra_kwargs = {
            'c_time': {
                'read_only': True
            },
        }

    def get_remaining(self, warehouse):
        """
        剩余天数 = 保质期天数 - (当前日期 - 生产日期)
        :param warehouse:
        :return:
        """
        return warehouse.quality_guarantee - (
                datetime.datetime.now(tz=pytz.UTC) - warehouse.date_of_manufacture
        ).days

    def get_remaining_day(self, warehouse):
        """
        保质期日期 = 生产日期+剩余天数
        """
        return warehouse.date_of_manufacture + datetime.timedelta(days=warehouse.quality_guarantee)

    def get_total(self, warehouse):
        total = self.get_warehouse1(warehouse) - self.get_warehouse2(warehouse)
        return total

    def get_warehouse1(self, warehouse):
        product_id = warehouse.product_id
        warehouse_info = Warehouse.objects.filter(stock_status=1).values("product").annotate(total=Sum("num")).all()
        total = warehouse_info.filter(product_id=product_id) and \
                warehouse_info.filter(product_id=product_id).get()["total"] or 0
        return total

    def get_warehouse2(self, warehouse):
        product_id = warehouse.product_id
        warehouse_info = Warehouse.objects.filter(stock_status=2).values("product").annotate(total=Sum("num")).all()
        total = warehouse_info.filter(product_id=product_id) and \
                warehouse_info.filter(product_id=product_id).get()["total"] or 0
        return total

    def get_warehouse1_time(self, warehouse):
        warehouse2_time = warehouse.c_time - datetime.datetime.now(datetime.timezone(datetime.timedelta(hours=+8)))
        return warehouse2_time.days

    def get_warehouse2_time(self, warehouse):
        warehouse2_time = datetime.datetime.now(datetime.timezone(datetime.timedelta(hours=+8))) - warehouse.c_time
        return warehouse2_time.days
